package com.hutiao.snowhb.actions.huodong;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.entities.PhotographerBean;
import com.hutiao.snowhb.resource.ConfigInfo;
import com.hutiao.snowhb.service.PhotographerService;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/huodong")
public class PhotographerAction extends ActionSupport {

	private static final long serialVersionUID = 6133186027454224378L;

	private static Logger logger = Logger.getLogger(PhotographerAction.class);
	
	private String result;
	
	@JSON(serialize=false)
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	private int lastId;

	public int getLastId() {
		return lastId;
	}

	public void setLastId(int lastId) {
		this.lastId = lastId;
	}

	@Autowired
	private PhotographerService photographerService;
	
	@JSON(serialize=false)
	public PhotographerService getPhotographerService() {
		return photographerService;
	}

	public void setPhotographerService(PhotographerService photographerService) {
		this.photographerService = photographerService;
	}
	
	List<PhotographerBean> photos;
	

	public List<PhotographerBean> getPhotos() {
		return photos;
	}

	public void setPhotos(List<PhotographerBean> photos) {
		this.photos = photos;
	}
	
	

	@Action(value="photographer_flow")
	public String flow() throws Exception {
		
		this.photos = this.photographerService.getPhotoFlow(-1);
		
		if (this.photos != null && this.photos.size()>0) {
			for (int i=0; i<this.photos.size(); i++) {
				this.photos.get(i).setPhotoDetailUrl(ConfigInfo.hdPhotoDetailUrl + "?photoId=" + this.photos.get(i).getId());
			}
			this.lastId = this.photos.get(this.photos.size()-1).getId();
		} else {
			this.lastId = -1;
		}
		
		return SUCCESS;
	}
	
	@Action(value="photographer_more",results = { @Result(type = "json") })
	public String loadMore() throws Exception {
		
		this.photos = this.photographerService.getPhotoFlow(this.lastId);
		
		if (this.photos != null && this.photos.size()>0) {
			for (int i=0; i<this.photos.size(); i++) {
				this.photos.get(i).setPhotoDetailUrl(ConfigInfo.hdPhotoDetailUrl + "?photoId=" + this.photos.get(i).getId());
			}
			this.lastId = this.photos.get(this.photos.size()-1).getId();
		} else {
			this.lastId = -1;
		}
		
		return SUCCESS;
	}
	
	
	@Action(value="photographer_album")
	public String album() throws Exception {
		
		this.photos = this.photographerService.getPhotoAlbum();
		
		return SUCCESS;
	}
	

}
